Imports System.IO.File
Module ModTextFile
    Public Enum mFileType
        TabDlm = 0
        TabComa = 1
        TabFix = 2
    End Enum

    Function IsTextFile(ByVal FilePath As String, Optional ByVal ValidChars As String = "", Optional ByVal delim As String = vbTab) As Boolean
        Try
            Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(FilePath)
                MyReader.TextFieldType = FileIO.FieldType.Delimited
                MyReader.SetDelimiters(delim)
                Dim currentRow As String()
                While Not MyReader.EndOfData
                    currentRow = MyReader.ReadFields()
                    If ((UCase(currentRow(0)(0)) >= "A") And (UCase(currentRow(0)(0)) <= "Z")) Or (UCase(currentRow(0)(0)) = "_") Then
                        Return True
                    Else
                        Return False
                    End If
                End While
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Function LoadTextFile(ByVal FilePath As String, Optional ByVal delim As String = vbTab) As DataTable
        Dim iCtr As Int32 = 1
        Dim iCol As Int32 = 1

        Dim dt As New DataTable
        Try
            Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(FilePath)
                MyReader.TextFieldType = FileIO.FieldType.Delimited
                MyReader.SetDelimiters(delim)
                Dim currentRow As String()
                Dim currentField As String
                Dim dr As DataRow
                While Not MyReader.EndOfData
                    Try
                        currentRow = MyReader.ReadFields()
                        If iCtr = 1 Then
                            dt = HeadRowTable(currentRow)
                        Else
                            iCol = 0
                            dr = dt.NewRow()
                            For Each currentField In currentRow
                                If iCol < currentRow.GetUpperBound(0) Then
                                    dr.Item(iCol) = CleanStr(currentField)
                                    iCol += 1
                                End If
                            Next
                            dt.Rows.Add(dr)
                            dt.AcceptChanges()
                        End If
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
                    End Try
                    iCtr += 1

                End While
            End Using

        Catch ex As Exception
            Throw ex
        End Try
        Return dt
    End Function

    Public Function CleanStr(ByVal sData As String) As String
        sData = Microsoft.VisualBasic.Replace(sData, "'", " ")
        sData = Microsoft.VisualBasic.Replace(sData, """", " ")
        Return sData
    End Function

    Private Function HeadRowTable(ByVal currentRow() As String) As DataTable
        Dim ObjectsTable As DataTable = New DataTable("DataTable")
        Try
            Dim currentField As String
          
            For Each currentField In currentRow
                If Trim(currentField) <> "" Then
                    Dim fObject As DataColumn = New DataColumn
                    fObject.DataType = System.Type.GetType("System.String")
                    fObject.ColumnName = Trim(currentField)
                    fObject.DefaultValue = ""
                    ObjectsTable.Columns.Add(fObject)
                End If
            Next
            Return ObjectsTable
        Catch ex As Exception
            ex.Source = "Invalid file format"
            Throw ex
        End Try
    End Function


End Module
